#!/usr/bin/python
#-*- coding: utf-8 -*-

#coding:utf8

import sys
reload(sys)
sys.setdefaultencoding('utf8')

import requests
import conf

class Message(object):
    """docstring for Message"""
    def __init__(self, phone, content):
        self.phone = phone
        self.content = content
        self.smsbao_user = conf.smsbao_user
        self.smsbao_passwd = conf.smsbao_passwd
        self.smsbao_api = conf.smsbao_api
        self.err = {
            0: '发送成功',
            30: '密码错误',
            40: '账号不存在',
            41: '余额不足',
            42: '帐号过期',
            43: 'IP地址限制',
            50: '内容含有敏感词',
            51: '手机号码不正确'
        }

    def send(self):
        payload = {
            'u':self.smsbao_user,
            'p':self.smsbao_passwd,
            'm':self.phone,
            'c':self.content
        }
        try:
            result = {}
            r = requests.get(self.smsbao_api,
                             params=payload,
                             timeout=10)
            text = r.text

            if text.isdigit():
                a = int(text)
                if a == 0:
                    result['code'] = 0
                    result['message'] = self.err.get(a,'发送成功')
                elif a in self.err:
                    result['code'] = a
                    result['message'] = self.err.get(a,'请求错误信息失败')
                else:
                    result['code'] = a
                    result['message'] = '接口未知错误'
            result['phone'] = self.phone
            return result

        except Exception, e:
            result = { 'code':200,'message':str(e)}
            return result

if __name__ == '__main__':
    m = Message('13434111885','这是我的第一个平台')
    print m.send()